package com.csoa.community.activity;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.collect.community.R;
import com.csoa.community.bean.IssueGroupBean;
import com.csoa.community.db.DataBaseDB;
import com.csoa.community.db.IssueGroupDB;
import com.csoa.community.db.LocalSpeciesInfoDB;
import com.csoa.community.singletons.GlobalVars;
import com.csoa.community.utils.IOUtils;
import com.csoa.community.utils.StringUtils;

public class LoadingActivity extends BaseActivity {

//	private PopWin pw;
	private IssueGroupBean mIssueGroup;
	InputStream inStream = null;
	String localspecies = "";
	private TextView mtvTitle;
	@Override
	public void initUI() {
		setContentView(R.layout.loading);
		DataBaseDB.getInst().createDB(this);
		 mtvTitle = (TextView)this.findViewById(R.id.tvTitleWindow);
		File file = new File(GlobalVars.getInst().getAppImgPath());
		if (!file.exists()) {
			file.mkdir();
		}
		initMapLocation();
//		pw = new PopWin(this);
//		pw.setOnConfirmClickListener(new OnConfirmClickListener() {
//			@Override
//			public void click() {
//				Intent intent = new Intent(LoadingActivity.this,
//						IssueGroupActivity.class);
//				intent.putExtra("IssueGroup", mIssueGroup);
//				startActivity(intent);
//				finish();
//			}
//		});
//		pw.setOnCancelClickListener(new OnCancelClickListener() {
//
//			@Override
//			public void click() {
//				Intent intent = new Intent(LoadingActivity.this,
//						MainActivity.class);
//				startActivity(intent);
//				finish();
//			}
//		});

	}

	private void showPopWindow() {

		/***************** 以下代码用来循环检测activity是否初始化完毕 ***************/
		Runnable showPopWindowRunnable = new Runnable() {

			@Override
			public void run() {
				// 得到activity中的根元素
				View view = findViewById(R.id.totalLayout);
				// 如何根元素的width和height大于0说明activity已经初始化完毕
				if (view != null && view.getWidth() > 0 && view.getHeight() > 0) {

					if (mIssueGroup == null) {
						Intent intent = new Intent(LoadingActivity.this,
								IssueGroupActivity.class);
						startActivity(intent);
						finish();
					} else {
//						pw.ShowWin(mIssueGroup);
						Intent intent = new Intent(LoadingActivity.this,
								MainActivity.class);
						startActivity(intent);
						finish();
					}

					// 停止检测
					mHandler.removeCallbacks(this);
				} else {
					// 如果activity没有初始化完毕则等待5毫秒再次检测
					mHandler.postDelayed(this, 5);
				}
			}
		};
		// 开始检测
		mHandler.postDelayed(showPopWindowRunnable, 4000);
		/****************** 以上代码用来循环检测activity是否初始化完毕 *************/
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		finish();
		System.exit(1);
		return false;
	}

	@Override
	public void initData() {
		mIssueGroup = IssueGroupDB.query();
		if(mIssueGroup != null) {
			mtvTitle.setVisibility(View.VISIBLE);
			mtvTitle.setText("欢迎" + mIssueGroup.unit + "课题组   " + mIssueGroup.writer + "登录   物种信息采集移动端");
		}
		showPopWindow();
		if(mIssueGroup == null) {
			try {
				localspecies = IOUtils.getFromAssets(this.getAssets().open("localspecies.db"));
				if(!StringUtils.isEmpty(localspecies)) {
					new Thread(){
						@Override
						public void run() {
							LocalSpeciesInfoDB.insert(localspecies);
						}
						
					}.start();
					
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	@Override
	protected void onResume() {
		startLocation();
		super.onResume();
	}
	
	@Override
	protected void onPause() {
		stopLocation();
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
//		pw.dismiss();
		destroyBMapManager();
	}

}
